package com.wsh.shop.head.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wsh.shop.common.pojo.ApiResponse;
import com.wsh.shop.common.pojo.ApiResponseEnum;
import com.wsh.shop.common.servlet.BaseServlet;
import com.wsh.shop.common.util.PageInfoUtil;
import com.wsh.shop.users.pojo.ACart;
import com.wsh.shop.users.pojo.AOrders;
import com.wsh.shop.users.pojo.AOrdersEnum;
import com.wsh.shop.users.pojo.AOrdersGoods;
import com.wsh.shop.users.pojo.AUsers;
import com.wsh.shop.users.pojo.AUsersEnum;
import com.wsh.shop.users.service.IGoodsDbService;
import com.wsh.shop.users.service.IUsersDbService;
import com.wsh.shop.users.service.impl.GoodsDbServiceImpl;
import com.wsh.shop.users.service.impl.UsersDbServiceImpl;

/**
 * 第二个Servlet 只存储和管理员息息相关的信息 登陆后的操作
 * 
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/head/OrdersHeadServlet")
public class OrdersHeadServlet extends BaseServlet
{
	private IGoodsDbService goodsDbService;
	private IUsersDbService usersDbService;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
		GoodsDbServiceImpl goodsDbServiceImpl = new GoodsDbServiceImpl();
		
		goodsDbServiceImpl.setUsersDbService(usersDbServiceImpl);
		usersDbServiceImpl.setGoodsDbService(goodsDbServiceImpl);
		
		this.goodsDbService = goodsDbServiceImpl ; 
		this.usersDbService = usersDbServiceImpl ; 
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("ordersList".equalsIgnoreCase(method))
		{
			/* 订单列表 */
			ordersList(request, response);
		}else if ("ordersUpdate".equalsIgnoreCase(method))
        {
            /* 订单更新 */
            ordersUpdate(request, response);
        }else if ("ordersUpdateSubmit".equalsIgnoreCase(method))
        {
            /* 订单更新 */
            ordersUpdateSubmit(request, response);
        }else if ("ordersGoodsList".equalsIgnoreCase(method))
		{
			/* 订单商品列表 */
			ordersGoodsList(request, response);
		}else if ("ordersGoodsUpdate".equalsIgnoreCase(method))
		{
			/* 订单商品更新 */
			ordersGoodsUpdate(request, response);
		}else if ("ordersInsert".equalsIgnoreCase(method))
		{
			/* 下单页面 */
			ordersInsert(request, response);
		}else if ("ordersInsertSubmit".equalsIgnoreCase(method))
		{
			/* 下单提交操作 */
			ordersInsertSubmit(request, response);
		}
	}
	
	private void ordersUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/head/orders/ordersInfo.jsp" ;
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        Map<String, Object> condMap = new HashMap<String, Object>();
        
        /* 按照id查询 */
        String id = request.getParameter("id");
        condMap.put("id", id);
        condMap.put("extend", "true");
        ApiResponse<AOrders> apiDbResponse = this.goodsDbService.findOneOrdersService(condMap);
        request.setAttribute("response", apiDbResponse.toJSON());
        
        /* 同时查询出订单商品信息 */
        AOrders orders = apiDbResponse.getDataOneJava();
        condMap.clear();
        condMap.put("ordersId", orders.getId());
        condMap.put("extend", "true");
        ApiResponse<AOrdersGoods> ordersGoodsApiResponse = this.goodsDbService.findCondListOrdersGoodsService(null, condMap);
        request.setAttribute("ordersGoodsResponse", ordersGoodsApiResponse.toJSON());
        
        String operType = request.getParameter("operType");
        boolean flag = false ; 
        if("update".equalsIgnoreCase(operType)) {
            String status = request.getParameter("status");
            orders.setStatus(Byte.valueOf(status));
            flag = true ; 
        }else if("pay".equalsIgnoreCase(operType)) {
            String payType = request.getParameter("payType");
            orders.setPayType(Byte.valueOf(payType));
            orders.setPayTime(new Date());
            orders.setStatus(AOrdersEnum.STATUS_YFK.getStatus());
            flag = true ; 
        }else if("takeGoods".equalsIgnoreCase(operType)) {
            /* 确认收货 */
            orders.setStatus(AOrdersEnum.STATUS_YSH.getStatus());
            flag = true ; 
        }
        /* 接收参数 */
        if(flag) {
            apiResponse = this.goodsDbService.updateOneOrdersService(orders);
        }
        /* 页面跳转 */
        response.sendRedirect(request.getContextPath() + "/head/OrdersHeadServlet?method=ordersList");
    }

    /**
	 * 打开下单页面
	 * @param request
	 * @param response
	 */
	private void ordersInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/head/orders/ordersInsert.jsp" ;
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", AUsersEnum.STATUS_ENABLE.getStatus());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 下单提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void ordersInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", AUsersEnum.STATUS_ENABLE.getStatus());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 接收必要信息 */
		String receName = request.getParameter("receName");
		String recePhone = request.getParameter("recePhone");
		String receAddress = request.getParameter("receAddress");
		String content = request.getParameter("content");
		
		/* 创建对象 */
		AOrders orders = new AOrders();
		orders.setUsersId(users.getId());
		orders.setReceName(receName);
		orders.setRecePhone(recePhone);
		orders.setReceAddress(receAddress);
		orders.setContent(content);
		
		/* 设置购物车 */
		List<ACart> cartList = apiResponse.getDataListJava();
		orders.setCartList(cartList);
		
		ApiResponse<Object> apiResponseDb = this.goodsDbService.saveOneOrdersService(orders);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			response.sendRedirect(request.getContextPath() + "/head/OrdersHeadServlet?method=ordersList");
			return ;
		}
		this.ordersInsert(request, response);
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	private void ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/head/orders/ordersInfo.jsp" ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AOrders> apiResponse = this.goodsDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 同时查询出订单商品信息 */
		AOrders orders = apiResponse.getDataOneJava();
		condMap.clear();
		condMap.put("ordersId", orders.getId());
		condMap.put("extend", "true");
		ApiResponse<AOrdersGoods> ordersGoodsApiResponse = this.goodsDbService.findCondListOrdersGoodsService(null, condMap);
		request.setAttribute("ordersGoodsResponse", ordersGoodsApiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			this.path = "/WEB-INF/jsp/head/orders/ordersUpdate.jsp" ;
		}else if("pay".equalsIgnoreCase(operType))
        {
            this.path = "/WEB-INF/jsp/head/orders/ordersPay.jsp" ;
        }
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/head/orders/ordersList.jsp" ; 
		
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AOrders> apiResponse = this.goodsDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	private void ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/head/orders/ordersGoodsInfo.jsp" ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AOrdersGoods> apiResponse = this.goodsDbService.findOneOrdersGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			this.path = "/WEB-INF/jsp/head/orders/ordersGoodsUpdate.jsp" ;
		}
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void ordersGoodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		this.path = "/WEB-INF/jsp/head/orders/ordersGoodsList.jsp" ; 
		
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AOrdersGoods> apiResponse = this.goodsDbService.findCondListOrdersGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}
}
